﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Xant.FlowEngine.Core;
using Xant.FlowEngine.Interface;

namespace Xant.FlowEngine.Actor
{
    public static class ActorHelper
    {
        private static IOrganizationLocator organizLocator;

        static ActorHelper()
        {
            var builder = new ContainerBuilder();
            builder.RegisterModule(new Autofac.Configuration.ConfigurationSettingsReader("autofac"));
            using (var container = builder.Build())
            {
                organizLocator = container.Resolve<IOrganizationLocator>();
            }
        }

        public static IOrganizationLocator OrganizLocator
        {
            get { return organizLocator; }
        }

        public static AbstractActor ParseActor(this XmlElement element)
        {
            AbstractActor actor = null;
            string type = element.GetAttribute("Type");
            if(type.Equals("Staff", StringComparison.CurrentCultureIgnoreCase))
            {
                string organizId = element.GetAttribute("Organization");
                string staffId = element.GetAttribute("Staff");
                IStaffMember member = organizLocator.FindStaffMember(organizId, staffId);
                actor = new StaffActor(member);
            }
            return actor;
        }

    }
}
